package otc.regate.serveur.controleur;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Classe d'analyse de fichier XML transmis par les clients
 *
 * @author Escop, Jacquet, Loiret, Marboeuf, Millet, Montferrand
 *
 */
public class Parser extends DefaultHandler {

    /**Latitude et longitude des coordonnees du client.*/
    private float latitude, longitude;

    /**Nom de la course.*/
    private String nomCourse;

    /**Identifiant du coureur.*/
    private int idSkipper;

    /**Date d'envoie du fichier XML.*/
    private String date;

    /**Balise courante.*/
    private String baliseCourante;

    /**
     * Constructeur.
     */
    public Parser() {
        super();
    }


    @Override
    public final void characters(final char[] ch, final int start,
            final int length)
            throws SAXException {

        if (baliseCourante.equals("desc")) {

            nomCourse = new String(ch, start, length);
        }

        if (baliseCourante == "id") {

            idSkipper = Integer.parseInt(new String(ch, start, length));
        }

        if (baliseCourante == "time") {

            date = new String(ch, start, length);
        }

    }


    @Override
    public final void startElement(final String uri, final String localName,
            final String name, final Attributes atts) throws SAXException {

        baliseCourante = name;

        if (name == "rtept") {

            this.latitude = Float.parseFloat(atts.getValue("lat"));
            this.longitude = Float.parseFloat(atts.getValue("lon"));
        }
    }

    @Override
    public void endDocument() throws SAXException {
    }

    @Override
    public void endElement(final String uri, final String localName,
            final String name)
            throws SAXException {
    }

    @Override
    public void endPrefixMapping(final String prefix) throws SAXException {
    }

    @Override
    public void ignorableWhitespace(final char[] ch, final int start,
            final int length)
            throws SAXException {
    }

    @Override
    public void processingInstruction(final String target,
            final String data)
            throws SAXException {
    }

    @Override
    public void skippedEntity(final String name) throws SAXException {
    }

    @Override
    public void startDocument() throws SAXException {
    }

    @Override
    public void startPrefixMapping(final String prefix, final String uri)
            throws SAXException {
    }

    /**
     * @return the latitude
     */
    public final float getLatitude() {
        return latitude;
    }

    /**
     * @param la the latitude to set
     */
    public final void setLatitude(final float la) {
        this.latitude = la;
    }

    /**
     * @return the longitude
     */
    public final float getLongitude() {
        return longitude;
    }

    /**
     * @param lo the longitude to set
     */
    public final void setLongitude(final float lo) {
        this.longitude = lo;
    }

    /**
     * @return the nomCourse
     */
    public final String getNomCourse() {
        return nomCourse;
    }

    /**
     * @param nomC the nomCourse to set
     */
    public final void setNomCourse(final String nomC) {
        this.nomCourse = nomC;
    }

    /**
     * @return the idSkipper
     */
    public final int getIdSkipper() {
        return idSkipper;
    }

    /**
     * @param idS the idSkipper to set
     */
    public final void setIdSkipper(final int idS) {
        this.idSkipper = idS;
    }

    /**
     * @return the date
     */
    public final String getDate() {
        return date;
    }

    /**
     * @param d the date to set
     */
    public final void setDate(final String d) {
        this.date = d;
    }

    /**
     * @return the baliseCourante
     */
    public final String getBaliseCourante() {
        return baliseCourante;
    }

    /**
     * @param b the baliseCourante to set
     */
    public final void setBaliseCourante(final String b) {
        this.baliseCourante = b;
    }
}